package com.ideabove.heart.manager;

import java.io.IOException;
import java.util.List;

import com.ideabove.heart.game.MoeState;
import com.ideabove.heart.game.framework.GameView;
import com.ideabove.heart.util.LogUtil;


import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Application;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;

public class AppManager extends Application{
	// Main GameView
	private GameView m_gameview;
	private Resources m_resources;
	private Activity m_activity;
	
	static MediaPlayer mPlayerBg;
	static Context playerContext;
	
	// GameState
	public MoeState m_gamestate;
	
	private int m_width;
	private int m_height;
	
	public static int activityCnt;
	
	
	public static void loadSound(final Context context){

		playerContext = context;
		
		AssetFileDescriptor afd;
		try {
			afd = context.getAssets().openFd("sound/bgm_vd.mp3");
			mPlayerBg = new MediaPlayer();
			mPlayerBg.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
			afd.close();			
			mPlayerBg.setLooping(true);
			mPlayerBg.prepare();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block					
			e.printStackTrace();		
		}
		
	}


	public static void setBgVolume(float volume){
		if(mPlayerBg != null){			
			mPlayerBg.setVolume(volume, volume);		
		}
	}
	
	public static void start(){
//		LogUtil.e("##Start");
		if(mPlayerBg != null){			
			mPlayerBg.start();		
		}
	}
	
	public static void pause(Activity act){
//		LogUtil.e("##Pause"+ act);
		if(mPlayerBg != null){
			mPlayerBg.pause();
		}
	}
	
	public static void stop(){
		if(mPlayerBg != null){
			mPlayerBg.stop();
		}
	}
	
	
	public void setGameView(GameView _gameview){
		m_gameview = _gameview;
	}
	public void setResources(Resources _resources ){
		m_resources = _resources;
	}
	public void setActivity(Activity _activity){
		m_activity = _activity;
	}
	public GameView getGameView(){
		return m_gameview;
	}
	public Resources getResources(){
		return m_resources;
	}
	public Activity getActivity(){
		return m_activity;
	}
	
	public void setSize(int x,int y){
		m_width = x;
		m_height = y;
	}
	public int getWidth(){
		return m_width;
	}
	public int getHeight(){
		return m_height;
	}
	
	public Bitmap getBitmap(int r){
		return BitmapFactory.decodeResource(m_resources,r);
	}
	
	private static AppManager s_instance;
	
	public static AppManager getInstance(){
		if(s_instance == null){
			s_instance = new AppManager();
		}
		return s_instance;
	}
}
